/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.client.event;


import com.google.common.eventbus.Subscribe;
import com.intelligent.ispc.client.core.view.global.MainStateBar;
import com.intelligent.ispc.client.utils.ApplicationBeanUtils;
import com.intelligent.ispc.client.utils.Colur;
import com.intelligent.ispc.common.utils.Threads;
import com.intelligent.ispc.constant.ProcessConstant;

import java.awt.*;

/**
 * Created by Peter on 2016/7/26.
 */
public class StatusBarListener {

    /**
     * Subscribe event for receiving message on system status bar.
     *
     * @param event StatusBarMsgChangeEvent
     */
    @Subscribe
    public void receivedMsg(StatusBarMsgChangeEvent event) {
        resetStatusValue(event);
    }

    /**
     * Subscribe event for updating message on system status bar.
     *
     * @param event StatusBarProcessChangeEvent
     */
    @Subscribe
    public void updateProcess(StatusBarProcessChangeEvent event) {
        updateProcessValue(event);
    }

    /**
     * Subscribe event for updating process url on system status bar.
     *
     * @param event StatusBarProcessUrlEvent
     */
    @Subscribe
    public void updateProcessUrl(StatusBarProcessUrlEvent event) {
        updateProcessUrlValue(event);
    }

    /**
     * Subscribe event for creating process on system status bar.
     *
     * @param event StatusBarProcessCreateTaskEvent
     */
    @Subscribe
    public void addProcess(StatusBarProcessCreateTaskEvent event) {
        addProcessTask(event);
    }


    /**
     * Subscribe event for finishing process on system status bar.
     *
     * @param event StatusBarProcessFinishTaskEvent
     */
    @Subscribe
    public void finishProcess(StatusBarProcessFinishTaskEvent event) {
        finishProcessTask(event);
    }

    private void resetStatusValue(StatusBarMsgChangeEvent event) {
        String message = event.getMessage();
        String level = event.getLevel();

        if (message == null) {
            message = "";
        }

        Color color = Colur.BLACK;
        if (StatusBarMsgChangeEvent.ERROR.equals(level)) {
            color = Colur.RED;
        }

        ApplicationBeanUtils.getBean(MainStateBar.class).setMessage(message, color);
    }

    private void updateProcessValue(StatusBarProcessChangeEvent event) {
        if (event.getLevel().equals(ProcessConstant.ERROR)) {
            ApplicationBeanUtils.getBean(MainStateBar.class).getProcessPanel().alarmTask(event.getTaskName(), event.getLog());
        } else {
            ApplicationBeanUtils.getBean(MainStateBar.class).getProcessPanel().updateTaskProcess(event.getTaskName(), event.getPoint(), event.getLog());
        }
    }

    private void addProcessTask(StatusBarProcessCreateTaskEvent event) {
        ApplicationBeanUtils.getBean(MainStateBar.class).getProcessPanel().addTask(event.getTaskName(), event.isShowLog());
    }

    private void updateProcessUrlValue(StatusBarProcessUrlEvent event) {
        ApplicationBeanUtils.getBean(MainStateBar.class).getProcessPanel().updateUrl(event.getTaskName(), event.getUrl());
    }

    private void finishProcessTask(StatusBarProcessFinishTaskEvent event) {
        final int sleepTime = 500;
        Threads.sleep(sleepTime);
        ApplicationBeanUtils.getBean(MainStateBar.class).getProcessPanel().finishTask(event.getTaskName(), event.isVisible());
    }
}
